<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Adminuser_model extends CI_Model {
  public function __construct() {
    parent::__construct();
  }

  public function do_login() {
    $username = $this->input->post('email');
    $password = $this->input->post('pass');

    $this->db->select()
      ->from('ci_adminusers cu')
      ->where('email', $username)
      ->where('password', hash('sha512', $password));
    $query = $this->db->get();

    if ($query->num_rows() == 1) {
      $result = $query->result_array();
      $data['email'] = $username;
      $this->set_session($data);
    } else {
      redirect('admin/adminuser/index');
    }
  }

  public function set_session($data) {
    $this->load->library('session');
    $data['lastlogin'] = time();

    $this->db->where('email', $data['email']);
    $this->db->update('ci_adminusers', $data);
    $this->session->set_userdata($data);

    redirect('admin/admindash/index');
  }
}